/*
 * Copyright 2002-2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lntea.lnblog.dao;

import com.lntea.lnblog.common.ConnectionFactory;
import com.lntea.lnblog.model.Post;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Post数据库操作类
 * @author lichao
 * @date 2016/6/30 16:12
 */
public class PostDao {

    public List<Post> getPostsByPage(Integer pageNum,Integer pageSize) throws SQLException {
        Connection con = ConnectionFactory.getConnection();
        con.setReadOnly(true);

        String sql = "select * from post p limit ?,?";
        PreparedStatement ps = con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
        ps.setInt(1,(pageNum - 1) * pageSize);
        ps.setInt(2, pageSize);

        ResultSet resultSet = ps.executeQuery();
        List<Post> postList = new ArrayList<Post>();
        while(resultSet.next()){
            Post post = new Post();
            postList.add(post);
            post.setId(resultSet.getLong("id"));
            String content = resultSet.getString("content");
            post.setContent(content);
        }
        return postList;
    }
}
